#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 1e5+10;
const int M = 3e5+10;
const int inf = 0x3f3f3f3f;

int n,m,cnt,fa[N][22],f[N],dep[N];
struct edge{int u,v,w;}e[M];
ll tot,ans;
int gf(int x){return f[x] == x ? x : f[x] = gf(f[x]);}
bool operator < (const edge a,const edge b){return a.w < b.w;}
vector<pii> G[N];
bool vis[M];
struct node{ll mx,mmx;node(ll _mx = -inf,ll _mmx = -inf):mx(_mx),mmx(_mmx){}}ve[N][22];

node merge(node x,node y){
	node t = x;
	if(y.mx > t.mx) {
		t.mmx = t.mx , t.mx = y.mx;
		if(y.mmx > t.mmx) t.mmx = y.mmx;
	}else if(y.mx == t.mx){
		if(y.mmx > t.mmx) t.mmx = y.mmx;
	}else if(y. mx > t.mmx) t.mmx = y.mx;
	return t;
}

void dfs(int x,int p){
	for(pii d : G[x]){
		int y = d.fi,w = d.se;
		if(y == p) continue;
		fa[y][0] = x; ve[y][0] = node(w,0); dep[y] = dep[x] + 1;
		dfs(y,x);
	}
}

node lca(int x,int y){
	node t;
	if(dep[x] < dep[y]) swap(x,y);
	for(int i = 20;~i;i--)
		if(dep[fa[x][i]] >= dep[y]){
			t = merge(t,ve[x][i]);
			x = fa[x][i];
		}
	if(x == y) return t;
	for(int i = 20;~i;i--)
		if(fa[x][i] != fa[y][i]){
			t = merge(t,ve[x][i]); t = merge(t,ve[y][i]);
			x = fa[x][i],y = fa[y][i];
		}
	t = merge(t,ve[x][0]); t = merge(t,ve[y][0]);
	return t;
}
	
signed main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in,m = in;
	for(int i = 1;i <= m;i++){
		int u = in,v = in,w = in;
		e[++cnt] = (edge){u,v,w};
	}
	sort(e+1,e+cnt+1);
	for(int i = 1;i <= n;i++) f[i] = i;
	for(int i = 1;i <= m;i++){
		int fx = gf(e[i].u),fy = gf(e[i].v);
		if(fx == fy) continue;
		f[fx] = fy; tot += e[i].w;
		G[e[i].u].pb(pii(e[i].v,e[i].w)); G[e[i].v].pb(pii(e[i].u,e[i].w));
		vis[i] = 1;
	}
	dfs(1,0);
	for(int k = 1;k <= 20;k++)
		for(int i = 1;i <= n;i++){
			fa[i][k] = fa[fa[i][k-1]][k-1];
			ve[i][k] = merge(ve[i][k-1],ve[fa[i][k-1]][k-1]);
		}
	ans = inf;
	for(int i = 1;i <= m;i++){
		if(vis[i]) continue; if(e[i].u == e[i].v) continue;
		node t = lca(e[i].u,e[i].v); 
		if(t.mx == e[i].w) {
			ans = min(ans,e[i].w - t.mmx);
		}else ans = min(ans,e[i].w - t.mx);
	}
	printf("%lld\n",tot+ans);
	return 0;
}
